package cn.org.timespace.easyframe.service.permission.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import cn.org.timespace.easyframe.dao.permission.EfUserRoleDao;
import cn.org.timespace.easyframe.model.permission.EfRole;
import cn.org.timespace.easyframe.model.permission.EfUser;
import cn.org.timespace.easyframe.model.permission.EfUserRole;
import cn.org.timespace.easyframe.service.permission.EfUserRoleService;
import cn.org.timespace.easyframe.util.generator.IdentityGenerator;

/**
 * <p>
 * 用户角色
 * </p>
 * 
 * @version 1.0, 11/01/18
 * @since 1.0
 * @author wubo
 */

public class EfUserRoleServiceImpl implements EfUserRoleService {

	private EfUserRoleDao efUserRoleDao;

	public List<EfUserRole> findByCondition(String roleId) {
		return efUserRoleDao.findByCondition(roleId);
	}

	public void save(EfUserRole transientInstance) {
		efUserRoleDao.save(transientInstance);
	}

	public void delete(EfUserRole transientInstance) {
		efUserRoleDao.delete(transientInstance);
	}

	public void saveUserRole(String roleId, String userIds) {
		
		if(!StringUtils.isEmpty(roleId) && !StringUtils.isEmpty(userIds)){
			//先删除
			efUserRoleDao.delete(roleId);
			
			//再保存
			String[] arrUserIds = userIds.split(",");
			for(int i=0;i<arrUserIds.length;i++){
				
				EfUserRole efUserRole = new EfUserRole();
				efUserRole.setUserRoleId(IdentityGenerator.getInstance().getId());
				
				EfRole efRole = new EfRole();
				efRole.setRoleId(roleId);
				efUserRole.setEfRole(efRole);
				
				EfUser efUser = new EfUser();
				efUser.setUserId(arrUserIds[i]);
				efUserRole.setEfUser(efUser);
				
				efUserRoleDao.save(efUserRole);				
			}
		}
		
	}

	public EfUserRoleDao getEfUserRoleDao() {
		return efUserRoleDao;
	}

	public void setEfUserRoleDao(EfUserRoleDao efUserRoleDao) {
		this.efUserRoleDao = efUserRoleDao;
	}
	
}
